package com.jiyinit.web.core.business.service;

import com.jiyinit.web.core.business.dao.TabFunRoleUserDao;
import com.jiyinit.web.core.business.entity.TabFunRoleUserEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.common.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lr on 2017/12/26.
 */
@Service("core.funRoleUserService")
public class FunRoleUserServiceImpl {

    @Resource(name = "core.tabFunRoleUserDao")
    private TabFunRoleUserDao tabFunRoleUserDao;

    public int updateRoleFunction(Integer orgId,Integer funRoleId,List<Integer> userIds){
        Integer id = UserUtil.getCurrentUser().getUserId();
        //删除原来的用户
        tabFunRoleUserDao.delete(orgId,funRoleId,id);

        //添加现在的用户
        List<TabFunRoleUserEntity> list = new ArrayList<>();
        for(Integer userId : userIds){
            TabFunRoleUserEntity entity = new TabFunRoleUserEntity();
            entity.setFunroleId(funRoleId);
            entity.setUserId(userId);
            entity.setOrgId(orgId);
            entity.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
            entity.setCreateId(id);
            entity.setCreateTime(new Date());
            list.add(entity);
        }
        return tabFunRoleUserDao.batchAdd(list);
    }
}
